package com.tusuo.stds.ui.similarity;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.view.KeyEvent;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tusuo.stds.R;
import com.tusuo.stds.rest.LevelRecordList;
import com.tusuo.stds.rest.SimilarityInfo;
import com.tusuo.stds.ui.BaseToolbarActivity;
import com.tusuo.stds.ui.ToolbarOwner;
import com.tusuo.stds.util.IntentUtils;
import com.tusuo.stds.util.Utils;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.res.StringRes;
import org.parceler.Parcels;

import java.util.ArrayList;
import java.util.List;

import at.grabner.circleprogress.CircleProgressView;
import at.grabner.circleprogress.UnitPosition;
import myandroid.library.util.ApplicationHelper;


@EActivity(R.layout.activity_similarity_result)
public class SimilarityResultActivity extends BaseToolbarActivity<SimilarityResultView, SimilarityResultPresenter>
                                implements SimilarityResultView {

    @ViewById(R.id.similarity_result_tv_title)
    TextView tvTitle;

    @ViewById(R.id.similarity_result_circleview)
    CircleProgressView circleProgressView;
    @ViewById(R.id.similarity_result_tv_clearance_time)
    TextView tvClearanceTime;
    @ViewById(R.id.similarity_result_tv_persent)
    TextView tvPersent;

    @ViewById(R.id.similarity_result_layout_back)
    LinearLayout layoutOnlyBack;

    @StringRes(R.string.toolbar_title_similarity_result)
    String toolbarTitle;
    @StringRes(R.string.err_msg_server)
    String strErrMsgServer;

    @AfterViews
    void afterViews() {
        setupUI(this);

        List<ToolbarOwner.MenuAction> menuActionList = new ArrayList<>();

        ToolbarOwner.Config config = new ToolbarOwner.Config(toolbarTitle, true, menuActionList);
        mToolbarOwner.setConfig(config);

        ApplicationHelper.getInstance().addActivity(this);
    }

    @NonNull
    @Override
    public SimilarityResultPresenter createPresenter() {
        return new SimilarityResultPresenter(this);
    }

    /**
     * 重定义返回键操作
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onBackPressed() {
        IntentUtils.toCurrentMainActivity(this);
    }

    @Override
    public void setSimilarityResult(SimilarityInfo similarityInfo) {
        tvTitle.setText(similarityInfo.Title);
        circleProgressView.setValue(similarityInfo.CorrectRate * 100);
        tvPersent.setText((int)(similarityInfo.CorrectRate * 100) + "");
        circleProgressView.setUnitPosition(UnitPosition.RIGHT_BOTTOM);
        tvClearanceTime.setText(getString(R.string.level_clearance_time, Utils.secondsToHumanReadable(similarityInfo.ClearanceTime)));
    }

    @Click(R.id.similarity_result_btn_back)
    void clickBackBtn() {
        if (Utils.UI.isFastDoubleClick()) {
            return;
        }
        Intent intent = new Intent(this,SimilarityTempActivity_.class);
        startActivity(intent);
        finish();
    }


    @Click(R.id.similarity_result_layout_details)
    void clickRecordDetailsBtn() {
        if (Utils.UI.isFastDoubleClick()) {
            return;
        }

        Intent intent = new Intent(this,SimilarityTempActivity_.class);
        startActivity(intent);
        finish();
//        setResult(6666);
//        finish();
    }
}
